Attribute VB_Name = "modServerTCP"
Option Explicit

'Sends data
Public Sub SendData(ByVal index As Integer, ByVal data As String)
    Call frmServer.Socket(index).SendData(data)
End Sub

'Sends Message To Client
Public Sub Send_PlainMsg(ByVal index As Integer, ByVal msg As String)
    Call frmServer.Socket(index).SendData("PlainMessage" & SEP_CHAR & msg)
End Sub

'Sends Error Message To Client
Public Sub Send_ErrorMsg(ByVal index As Integer, ByVal errormsg As String)
    Call frmServer.Socket(index).SendData("ErrorMessage" & SEP_CHAR & errormsg)
End Sub


'If user logins sends command to client that user is logged in now
Public Sub Send_UserAuthenticated(ByVal index As Integer, ByVal username As String)
    Call frmServer.Socket(index).SendData("UserAuthenticated" & SEP_CHAR & username)
End Sub

'Sends news to client
Public Sub Send_News(ByVal index As Integer)
    Dim news, title As String
    title = ReadINI("NEWS", "TITLE", App.Path & "\News\News.ini")
    news = ReadINI("NEWS", "NEWS", App.Path & "\News\News.ini")
    Call SendData(index, "NEWS" & SEP_CHAR & title & SEP_CHAR & news)
End Sub

Public Function isLoggedIn(ByVal index As Integer) As Boolean
    If index < 1 Or index > MAX_PLAYERS Then _
        Exit Function
        
    If isConnected(index) Then
        If Trim$(Player(index).Login) <> vbNullString Then
            isLoggedIn = True
        End If
    End If
End Function

Public Function isConnected(ByVal index As Integer) As Boolean
    On Error GoTo setfalse
    If frmServer.Socket(index).LocalPort = SERVERPORT Then
        isConnected = True
        Exit Function
    End If
setfalse:
    isConnected = False
    Exit Function
End Function

Public Function isPlaying(ByVal index As Integer) As Boolean
    If index < 1 Or index > MAX_PLAYERS Then _
        Exit Function
    
    If (isConnected(index)) And (Player(index).InGame) Then _
        isPlaying = True _
    Else _
        isPlaying = False
End Function

Public Sub Disconnect(ByVal index As Integer)
    If index < 1 Or index > MAX_PLAYERS Then _
        Exit Sub
    
    If isConnected(index) Then
        frmServer.Socket(index).Close
        Unload frmServer.Socket(index)
    End If
End Sub

Public Sub makeConnection(ByVal index As Integer)
    If index < 1 Or index > MAX_PLAYERS Then _
        Exit Sub
        
    If Not isConnected(index) Then
        Call Load(frmServer.Socket(index))
        frmServer.Socket(index).LocalPort = SERVERPORT
    End If
End Sub
